package com.yanqu.road.server.cmd.activity.summergames;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summergames.SummerGamesSelectReward;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.entity.activity.summergames.config.SummerGamesConvertInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.S_SUMMER_GAMES_CONVERT_MEDALS, desc = "兑换")
@CmdApi(reqMsg = SummerGamesProto.SummerGamesConvertReqMsg.class, respMsg = SummerGamesProto.SummerGamesConvertRespMsg.class)
public class SummerGamesConvertCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInTimeAndShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        SummerGamesProto.SummerGamesConvertReqMsg req = SummerGamesProto.SummerGamesConvertReqMsg.parseFrom(packet.getBytes());

        int reqTimes = 1;

        if(req.getTimes() > reqTimes){
            reqTimes = req.getTimes();
        }

        SummerGamesConvertInfo convertInfo = config.getSummerGamesConvertInfo(req.getConvertId());
        if(convertInfo == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        SummerGamesUserData userData = player.getModule(SummerGamesModule.class).getUserData();
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        Integer hadConvertTimes = userData.getConvertNumMap().getOrDefault(convertInfo.getId(), 0);
        if(hadConvertTimes + reqTimes > convertInfo.getNumLimit()){
            ret = GameErrorCode.E_SUMMER_GAMES_CONVERT_TIMES_LIMIT;
            return;
        }

        //自选奖励有吗
        SummerGamesSelectReward summerGamesSelectReward = null;
        Integer allSelectLimit = null;
        Integer hadSelectTimes = null;
        if(!convertInfo.getFlexiExchangeRewardMap().isEmpty()) {
            summerGamesSelectReward = userData.getConvertSelectMap().get(req.getConvertId());
            if (summerGamesSelectReward == null || StringUtils.isNullOrEmpty(summerGamesSelectReward.getSelect())) {
                ret = GameErrorCode.E_HONG_XIAN_REWARD_NO_SELECTED;
                return;
            }

            allSelectLimit = convertInfo.getFlexiExchangeRewardMap().get(summerGamesSelectReward.getSelect());
            //剩余次数够吗
            hadSelectTimes = summerGamesSelectReward.getSelectMap().getOrDefault(summerGamesSelectReward.getSelect(), 0);

            if(reqTimes + hadSelectTimes > allSelectLimit){
                ret = GameErrorCode.E_HONG_XIAN_REWARD_SELECTED_MAX_LIMIT;
                return;
            }
        }


        Property cost = new Property();
        for (Integer integer : convertInfo.getExchangeItemList()) {
            cost.addProperty(integer, BigInteger.ONE);
        }
        cost.rideProperty(reqTimes);

        if(cost.isNothing()){
            ret = GameErrorCode.E_GAME_ERROR;
            return;
        }

        if(!player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.SummerGamesActivity, eLogMoneyType.SummerGamesActivityConvertConsume)){
            ret = GameErrorCode.E_SUMMER_GAMES_PLEASE_COMPLETE_COLLECT;
            return;
        }

        Property reward = PropertyHelper.parseStringToProperty(convertInfo.getExchangeReward());

        //自选奖励
        if(summerGamesSelectReward != null) {
            reward.addProperty(PropertyHelper.parseStringToProperty(summerGamesSelectReward.getSelect()));
        }

        reward.rideProperty(reqTimes);

        if(summerGamesSelectReward != null) {
            summerGamesSelectReward.getSelectMap().put(summerGamesSelectReward.getSelect(), reqTimes + hadSelectTimes);

            if (reqTimes + hadSelectTimes >= allSelectLimit) {
                summerGamesSelectReward.setSelect("");
            }
        }

        userData.addConvertTimes(convertInfo.getId(), reqTimes);

        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.SummerGamesActivity, eLogMoneyType.SummerGamesActivityConvertReward);

        SummerGamesProto.SummerGamesConvertRespMsg.Builder clientMsg = SummerGamesProto.SummerGamesConvertRespMsg.newBuilder();
        clientMsg.setRet(0);
        clientMsg.setReward(PropertyHelper.parsePropertyToString(reward));

        Integer curTimes = userData.getConvertNumMap().getOrDefault(convertInfo.getId(), 0);
        SummerGamesProto.SummerGamesConvertTimesTemp.Builder convertBuilder = SummerGamesProto.SummerGamesConvertTimesTemp.newBuilder();
        convertBuilder.setId(convertInfo.getId());
        convertBuilder.setNum(curTimes);
        clientMsg.setConvertTimes(convertBuilder);
        if(summerGamesSelectReward != null){
            clientMsg.setSelectReward(SummerGamesActivityMgr.buildSummerGamesSelectRewardTemp(summerGamesSelectReward));
        }
        player.sendPacket(Protocol.U_SUMMER_GAMES_CONVERT_MEDALS, clientMsg);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_GAMES_CONVERT_MEDALS, SummerGamesProto.SummerGamesConvertRespMsg.newBuilder().setRet(ret));
        }
    }
}
